<?php
/**
 * 后台管理员操作处理代码
 */
// 引入项目初始化文件
require '../include/init.php';
// 获取操作的标识act的值
$act = isset($_GET['act']) ? $_GET['act'] : $_POST['act'];
// 处理非法操作
$action = ['add', 'edit','del'];
if(!in_array($act, $action)){
	msg('非法操作');
}
//添加新闻
else if($act == 'add'){
	extract($_POST);
	$title = trim($title);
	$content = addslashes($content);
	$time = time();
	$author = trim($author);
	$keyword = trim($keyword);
	$desc = trim($desc);
	$cate_id = $parent_id;
	$files = $_FILES['img'];
	$dir = '../uploads';
	$types = ['jpg','jpeg','gif','png','bmp'];
	$img = uploadFile($files,$dir,$types);
	$sql = "INSERT into tk_new (title,content,ntime,author,keyword,descp,titleimg,cate_id) values('$title','$content','$time','$author','$keyword','$desc','$img','$cate_id')";
	mysql_query($sql);
	if(mysql_affected_rows()>0){
		msg('添加成功','new_list.php');
	}else{
		msg('添加失败','new_add.php');
	}
	
}
//修改新闻
else if($act == 'edit'){
	extract($_POST);
	$title = trim($title);
	$content = addslashes($content);
	$author = trim($author);
	$keyword = trim($keyword);
	$desc = trim($desc);
	$cate_id = $parent_id;
	$files = $_FILES['img'];
	$dir = '../uploads';
	$types = ['jpg','jpeg','gif','png','bmp'];
	$img = uploadFile($files,$dir,$types);
	$sql = "UPDATE  tk_new set title='$title',content='$content',author='$author',keyword='$keyword',descp='$desc',cate_id='$cate_id',titleimg='$img' where nid =$id";
		mysql_query($sql);
	if(mysql_affected_rows()>0){
		msg('修改成功','new_list.php');
	}else{
		msg('修改失败','new_add.php');
	}
}
//删除新闻
else if($act == 'del'){
	//数据处理
	$id = $_GET['id'];
	$sql = "DELETE from tk_new where nid =$id";
	mysql_query($sql);
	if(mysql_affected_rows()>0){
		msg('删除成功','new_list.php');
	}else{
		msg('删除失败','new_list.php');
	}

}
?>